package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.core.domain.entity.SysDeptEnterprise;
import com.ruoyi.system.mapper.SysDeptEnterpriseMapper;
import com.ruoyi.system.service.ISysDeptEnterpriseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 企业微信部门Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-04
 */
@Service
public class SysDeptEnterpriseServiceImpl extends ServiceImpl<SysDeptEnterpriseMapper, SysDeptEnterprise> implements ISysDeptEnterpriseService {

    @Override
    public List<TreeSelect> selectDeptTreeList(SysDept dept) {


        List<SysDeptEnterprise> deptList = this.list();
        List<TreeSelect> treeSelects = buildDeptTreeSelect(deptList);
        return treeSelects;


    }



    public List<TreeSelect> buildDeptTreeSelect(List<SysDeptEnterprise> depts)
    {
        List<SysDeptEnterprise> deptTrees = buildDeptTree(depts);

        List<TreeSelect> selectList = deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
        return selectList;
    }
    public List<SysDeptEnterprise> buildDeptTree(List<SysDeptEnterprise> depts)
    {
        List<SysDeptEnterprise> returnList = new ArrayList<SysDeptEnterprise>();
        List<Integer> tempList = depts.stream().map(SysDeptEnterprise::getId).collect(Collectors.toList());
        for (SysDeptEnterprise dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<SysDeptEnterprise> list, SysDeptEnterprise t)
    {
        // 得到子节点列表
        List<SysDeptEnterprise> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDeptEnterprise tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<SysDeptEnterprise> getChildList(List<SysDeptEnterprise> list, SysDeptEnterprise t)
    {
        List<SysDeptEnterprise> tlist = new ArrayList<SysDeptEnterprise>();
        Iterator<SysDeptEnterprise> it = list.iterator();
        while (it.hasNext())
        {
            SysDeptEnterprise n = (SysDeptEnterprise) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDeptEnterprise> list, SysDeptEnterprise t)
    {
        return getChildList(list, t).size() > 0;
    }
}
